package com.nan.gbd.library.extractor;

import android.media.MediaExtractor;
import android.media.MediaFormat;

import com.nan.gbd.library.utils.Logger;

import java.io.IOException;

public abstract class Extractor extends Thread {
    //视频文件路径
    protected String filePath;
    //是否播放
    protected boolean isPlaying;
    //是否暂停
    protected boolean isPause;
    //播放时间起点
    protected long seekToUs = -1;
    //监听器
    protected ExtractorListener extractorListener;
    //解封装器
    protected MediaExtractor mediaExtractor;
    //最大帧大小
    protected int maxInputSize;

    long startMs;

    //开始解封装
    protected abstract void extractorMediaData();


    public Extractor(String filePath) {
        this.filePath = filePath;
        mediaExtractor = new MediaExtractor();
        try {
            mediaExtractor.setDataSource(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPlaying(boolean playing, long startMs) {
        isPlaying = playing;
        this.startMs = startMs;
    }

    public void setPause(boolean pause) {
//        if (pause) {
//            pauseMs = System.currentTimeMillis();
//        } else {
//            pauseTot = pauseTot + System.currentTimeMillis() - pauseMs;
//        }
        isPause = pause;
    }

    public void setSeekToUs(long seekToUs) {
        this.seekToUs = seekToUs;
    }

    public void setExtractorListener(ExtractorListener extractorListener) {
        this.extractorListener = extractorListener;
    }

    /**
     * 获取媒体类型的轨道
     *
     * @param extractor
     * @param mediaType
     * @return
     */
    protected int getTrackIndex(MediaExtractor extractor, String mediaType) {
        int trackIndex = -1;
        for (int i = 0; i < extractor.getTrackCount(); i++) {
            MediaFormat mediaFormat = extractor.getTrackFormat(i);
            String mime = mediaFormat.getString(MediaFormat.KEY_MIME);
            if (mime.startsWith(mediaType)) {
                trackIndex = i;
                break;
            }
        }
        return trackIndex;
    }

    public float getFileDurationUs() {
        try {
            mediaExtractor.setDataSource(filePath);
            // 获取视频所在轨道
            int videoTrackIndex = getTrackIndex(mediaExtractor, "video/");
            if (videoTrackIndex < 0) return 0f;
            MediaFormat mediaFormat = mediaExtractor.getTrackFormat(videoTrackIndex);
            return mediaFormat.getLong(MediaFormat.KEY_DURATION) / 1000000;
        } catch (IOException e) {
            e.printStackTrace();
            return 0f;
        }
    }

    public void release() {
        if (!isInterrupted())
            interrupt();
        if (mediaExtractor != null) {
            mediaExtractor.release();
            mediaExtractor = null;
        }
        filePath = null;
        isPlaying = false;
        isPause = false;
        seekToUs = -1;
        extractorListener = null;
        maxInputSize = -1;
    }
}
